function r60_reader(ncid, ids, jpos) result(sim)

    use iasing_osse
    use netcdf

    implicit none
    ! Arguments
    integer         :: ncid
    integer(JPI4)   :: jpos
    type(r4a_ids)   :: ids
    type(r60_dat)   :: sim

    ! input/output status 
    integer :: io
    ! counters and markers for netcdf reading
    integer(JPI4), dimension(1) :: st1d, co1d
    integer(JPI4), dimension(2) :: st2d, co2d

    ! - setting markers
    st1d = jpos
    co1d = 1
    st2d = (/1,jpos/)

    ! Reading dimension group
    if (ids%dim%gid .gt.0) then
        io = nf90_get_var(ids%dim%gid, ids%dim%atm, sim%dim%nopro, &
                        start=st1d)
        if (ids%dim%lev .gt. 0) then
            if (io /= NF90_NOERR) then
                print*, "error reading 'plev' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'plev' not read..."
        endif
        io = nf90_get_var(ids%dim%gid, ids%dim%lev, sim%dim%lev)
        if (ids%dim%lev .gt. 0) then
            if (io /= NF90_NOERR) then
                print*, "error reading 'plev' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'plev' not read..."
        endif
        if (ids%dim%wia .gt. 0) then
            io = nf90_get_var(ids%dim%gid, ids%dim%wia, sim%dim%wia)
            if (io /= NF90_NOERR) then
                print*, "error reading 'wnia' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'wnia' not read..."
        endif
        if (ids%dim%wng .gt. 0) then
            io = nf90_get_var(ids%dim%gid, ids%dim%wng, sim%dim%wng)
            if (io /= NF90_NOERR) then
                print*, "error reading 'wnng' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'wnng' not read..."
        endif
        if (ids%dim%ocl .gt. 0) then
            io = nf90_get_var(ids%dim%gid, ids%dim%ocl, sim%dim%ocl)
            if (io /= NF90_NOERR) then
                print*, "error reading 'ochl' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'ochl' not read..."
        endif
    endif

    ! Coordinates group
    if (ids%coo%gid .gt. 0) then
        if (ids%coo%lon .gt. 0) then
            io = nf90_get_var(ids%coo%gid, ids%coo%lon, sim%coo%lon, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'lon' variable"
                print*, nf90_strerror(io)
                print*
            else
                if (sim%coo%lon>180.) sim%coo%lon = sim%coo%lon-360.
            endif
            
        else 
            print*, "Variable 'lon' not read..."
        endif
        if (ids%coo%lat .gt. 0) then
            io = nf90_get_var(ids%coo%gid, ids%coo%lat, sim%coo%lat, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'lat' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'lat' not read..."
        endif
        if (ids%coo%ele .gt. 0) then
            io = nf90_get_var(ids%coo%gid, ids%coo%ele, sim%coo%ele, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'ele' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'ele' not read..."
        endif
        if (ids%coo%dat .gt. 0) then
            io = nf90_get_var(ids%coo%gid, ids%coo%dat, sim%coo%dat, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'dat' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'dat' not read..."
        endif
        if (ids%coo%tim .gt. 0) then
            io = nf90_get_var(ids%coo%gid, ids%coo%tim, sim%coo%tim, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'tim' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'tim' not read..."
        endif
    endif

    ! Observation parameters group
    if (ids%opa%gid .gt. 0) then
        if (ids%opa%psu .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%psu, sim%opa%psu, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'psu' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'psu' not read..."
        endif
        if (ids%opa%tsu .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%tsu, sim%opa%tsu, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'tsu' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'psu' not read..."
        endif
        if (ids%opa%iza .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%iza, sim%opa%iza, st1d) 
            if (io /= NF90_NOERR) then
                print*, "error reading 'iza' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'iza' not read..."
        endif
        if (ids%opa%iaa .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%iaa, sim%opa%iaa, st1d) 
            if (io /= NF90_NOERR) then
                print*, "error reading 'iaa' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'iaa' not read..."
        endif
        if (ids%opa%sza .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%sza, sim%opa%sza, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'sza' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'sza' not read..."
        endif
        if (ids%opa%saa .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%saa, sim%opa%saa, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'saa' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'saa' not read..."
        endif
        if (ids%opa%lsm .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%lsm, sim%opa%lsm, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'lsm' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'lsm' not read..."
        endif
        if (ids%opa%clc .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%clc, sim%opa%clc, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'clc' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'clc' not read..."
        endif
        if (ids%opa%emi .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%emi, sim%opa%emi, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'emi' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'emi' not read..."
        endif
        if (ids%opa%ert .gt. 0) then
            co2d = (/NIANG,1/)
            io = nf90_get_var(ids%opa%gid, ids%opa%ert, sim%opa%ert, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'ert' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'ert' not read..."
        endif
        if (ids%opa%rst .gt. 0) then
            io = nf90_get_var(ids%opa%gid, ids%opa%rst, sim%opa%rst, st1d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'rst' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'rst' not read..."
        endif
    endif

    ! Vertical profiles group
    if (ids%vpr%gid .gt. 0) then
        co2d = (/NLM60,1/)
        if (ids%vpr%tem .gt. 0) then
            io = nf90_get_var(ids%vpr%gid, ids%vpr%tem, sim%vpr%tem, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'tem' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'tem' not read..."
        endif
        if (ids%vpr%q   .gt. 0) then
            io = nf90_get_var(ids%vpr%gid, ids%vpr%q, sim%vpr%q, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'q' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'q' not read..."
        endif
        if (ids%vpr%co2 .gt. 0) then
            io = nf90_get_var(ids%vpr%gid, ids%vpr%co2, sim%vpr%co2, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'co2' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'co2' not read..."
        endif
        if (ids%vpr%o3  .gt. 0) then
            io = nf90_get_var(ids%vpr%gid, ids%vpr%o3, sim%vpr%o3, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'o3' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'o3' not read..."
        endif
        if (ids%vpr%co  .gt. 0) then
            io = nf90_get_var(ids%vpr%gid, ids%vpr%co, sim%vpr%co, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'co' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'co' not read..."
        endif
        if (ids%vpr%ch4 .gt. 0) then
            io = nf90_get_var(ids%vpr%gid, ids%vpr%ch4, sim%vpr%ch4, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'ch4' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'ch4' not read..."
        endif
        if (ids%vpr%so2 .gt. 0) then
            io = nf90_get_var(ids%vpr%gid, ids%vpr%so2, sim%vpr%so2, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'so2' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'so2' not read..."
        endif
    endif

    ! Cloud information profiles group
    if (ids%cld%gid .gt. 0) then
        co2d = (/NLM60,1/)
        if (ids%cld%cc .gt. 0) then
            io = nf90_get_var(ids%cld%gid, ids%cld%cc, sim%cld%cc, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'cc' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'cc' not read..."
        endif
        if (ids%cld%iwc .gt. 0) then
            io = nf90_get_var(ids%cld%gid, ids%cld%iwc, sim%cld%iwc, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'iwc' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'iwc' not read..."
        endif
        if (ids%cld%lwc .gt. 0) then
            io = nf90_get_var(ids%cld%gid, ids%cld%lwc, sim%cld%lwc, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'lwc' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'lwc' not read..."
        endif
        if (ids%cld%rwc .gt. 0) then
            io = nf90_get_var(ids%cld%gid, ids%cld%rwc, sim%cld%rwc, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'rwc' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'rwc' not read..."
        endif
        if (ids%cld%swc .gt. 0) then
            io = nf90_get_var(ids%cld%gid, ids%cld%swc, sim%cld%swc, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'swc' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'swc' not read..."
        endif
    endif


    ! Radiances group
    if (ids%rbt%gid .gt. 0) then
        if (ids%rbt%ria .gt. 0) then
            co2d = (/NIASI,1/)
            io = nf90_get_var(ids%rbt%gid, ids%rbt%ria, sim%rbt%ria, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'ria' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'ria' not read..."
        endif
        if (ids%rbt%rng .gt. 0) then
            co2d = (/NIANG,1/)
            io = nf90_get_var(ids%rbt%gid, ids%rbt%rng, sim%rbt%rng, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'rng' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'rng' not read..."
        endif
        if (ids%rbt%ngb .gt. 0) then
            co2d = (/NIANG,1/)
            io = nf90_get_var(ids%rbt%gid, ids%rbt%ngb, sim%rbt%ngb, &
                start=st2d, count=co2d)
            if (io /= NF90_NOERR) then
                print*, "error reading 'ngb' variable"
                print*, nf90_strerror(io)
                print*
            endif
        else 
            print*, "Variable 'ngb' not read..."
        endif
    endif

    ! Observations group
    if (ids%obs%gid .gt. 0) then 
        if (ids%obs%oma .gt. 0) then
            io = nf90_get_var(ids%obs%gid, ids%obs%oma, sim%obs%oma, st1d)
            if (io == NF90_NOERR) then
                ! Read date if available
                if (sim%obs%oma == 1) then
                    co2d = (/NCHAN,1/)
                    if (ids%obs%obt .gt. 0) then
                        io = nf90_get_var(ids%obs%gid, ids%obs%obt, sim%obs%obt, &
                            start=st2d, count=co2d)
                        if (io /= NF90_NOERR) then
                            print*, "error reading 'obt' variable"
                            print*, nf90_strerror(io)
                            print*
                        endif
                    else
                        print*, "Variable 'obt' not read..."
                    endif
                    if (ids%obs%obi .gt. 0) then
                        io = nf90_get_var(ids%obs%gid, ids%obs%obi, sim%obs%obi, &
                            start=st2d, count=co2d)
                        if (io /= NF90_NOERR) then
                            print*, "error reading 'obi' variable"
                            print*, nf90_strerror(io)
                            print*
                        endif
                    else
                        print*, "Variable 'obi' not read..."
                    endif
                    if (ids%obs%ofd .gt.0) then
                        io = nf90_get_var(ids%obs%gid, ids%obs%ofd, sim%obs%ofd, &
                            start=st2d, count=co2d)
                        if (io /= NF90_NOERR) then
                            print*, "error reading 'ofd' variable"
                            print*, nf90_strerror(io)
                            print*
                        endif
                    else
                        print*, "Variable 'ofd' not read..."
                    endif
                else
                    sim%obs%obt = -999.9
                    sim%obs%obt = -999.9
                    sim%obs%obt = -999.9
                endif
                
            else
                print*, "error reading 'oma' variable"
                print*, nf90_strerror(io)
                print*
                sim%obs%oma = -1
            endif
        else 
            print*, "Variable 'oma' not read..."
        endif
    endif

    return

end function

